package com.hutiao.snowhb.actions.mm;

import java.sql.SQLException;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import com.hutiao.snowhb.common.WeixinUtil;
import com.hutiao.snowhb.entities.WeixinUserInfo;
import com.hutiao.snowhb.entities.SnowMMComment;
import com.hutiao.snowhb.service.SnowMMService;
import com.opensymphony.xwork2.ActionSupport;

@Namespace("/snowmm")
public class SnowMMCommentAction extends ActionSupport {

	private static final long serialVersionUID = -3472371121635843268L;
	
	private static Logger logger = Logger.getLogger(SnowMMCommentAction.class);
	
	@Autowired
	private SnowMMService mmService;
	
	private int mcId;
	
	private SnowMMComment comment;
	
	//private WeixinUserInfo wxUser;
	
	//private String openId;
	
	private String result;
	
	public int getMcId() {
		return mcId;
	}

	public void setMcId(int mcId) {
		this.mcId = mcId;
	}

	@JSON(serialize=false)
	public SnowMMComment getComment() {
		return comment;
	}

	public void setComment(SnowMMComment comment) {
		this.comment = comment;
	}

//	public WeixinUserInfo getWxUser() {
//		return wxUser;
//	}
//
//	public void setWxUser(WeixinUserInfo wxUser) {
//		this.wxUser = wxUser;
//	}

//	@JSON(serialize=false)
//	public String getOpenId() {
//		return openId;
//	}
//
//	public void setOpenId(String openId) {
//		this.openId = openId;
//	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

//	@Action(value="weixin_userinfo",results = { @Result(type = "json") })
//	public String WeixinUserInfo() {
//		
//		try {
//			this.wxUser = WeixinUtil.getUserInfo(this.openId);
//		} catch (Exception e) {
//			this.result = "服务器开小差了，请稍后再试...";
//			logger.error("Exception", e);
//			return ERROR;
//		}
//		
//		return SUCCESS;
//	}
	
	
	@Action(value="comment_post",results = { @Result(type = "json") })
	public String postComment() {
		
		try {
			this.mcId = this.mmService.saveComment(this.comment);
		} catch (SQLException e) {
			this.result = "error";
			logger.error("Exception", e);
			return SUCCESS;
		}
		
		this.result = "success";
		return SUCCESS;
	}
	
	@Action(value="comment_del",results = { @Result(type = "json") })
	public String deleteComment() {
		
		SnowMMComment delcomm = this.mmService.getComment(this.mcId);
		
		if (delcomm == null) {
			logger.error("无法找到mcid＝"+this.mcId+"的mm评论记录，取消删除操作。");
			return SUCCESS;
		}
		
		this.mmService.delComment(delcomm);
		
		return SUCCESS;
	}

}
